package com.hakim.wfb.customer.service.impl;

import com.hakim.wfb.bean.User;
import com.hakim.wfb.bean.UserWallet;
import com.hakim.wfb.customer.enums.ErrorCodeEnum;
import com.hakim.wfb.customer.params.wallet.GenerateUserWalletParameter;
import com.hakim.wfb.customer.params.wallet.GetCoinMarketPriceParam;
import com.hakim.wfb.customer.params.wallet.GetUserWalletParameter;
import com.hakim.wfb.customer.params.wallet.WithdrawWalletParam;
import com.hakim.wfb.customer.result.wallet.CoinMarketPriceResult;
import com.hakim.wfb.customer.result.wallet.UserWalletAddressResult;
import com.hakim.wfb.customer.result.wallet.UserWalletView;
import com.hakim.wfb.customer.result.wallet.WalletWithdrawResult;
import com.hakim.wfb.customer.service.WalletService;
import com.hakim.wfb.customer.service.biz.TransPointBizService;
import com.hakim.wfb.customer.service.biz.WithdrawBizService;
import com.hakim.wfb.dao.UserDao;
import com.hakim.wfb.dao.UserWalletDao;
import com.hkfs.fundamental.api.data.ObjectResultEx;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/7/31.
 */
@Service("walletService")
public class WalletServiceImpl implements WalletService {

    private static Logger logger = LoggerFactory.getLogger(WalletServiceImpl.class);

    @Autowired
    private TransPointBizService transPointBizService;

    @Autowired
    private WithdrawBizService withdrawBizService;

    @Autowired
    private UserWalletDao userWalletDao;

    @Autowired
    private UserDao userDao;

    /**
     * 获取用户钱包地址
     *
     * @param param
     * @return
     */
    @Override
    @Transactional
    public ObjectResultEx<UserWalletView> getUserWallet(GetUserWalletParameter param) {

        ObjectResultEx<UserWalletView> resultEx = new ObjectResultEx<>();
        User user = userDao.getById(param.getUserId());
        if (user == null) {
            resultEx.setRetCode(ErrorCodeEnum.USER_NOT_EXIST_ERROR.getCode());
            resultEx.setRetMsg(ErrorCodeEnum.USER_NOT_EXIST_ERROR.getMessage());
            return resultEx.makeFailedResult();
        }
        //先获取钱包地址，不存在然后生成
        GetUserWalletParameter getUserWalletParameter = new GetUserWalletParameter();
        getUserWalletParameter.setUserId(param.getUserId());
        List<UserWalletView> userWalletViewList = userWalletDao.getUserWallet(getUserWalletParameter);
        boolean needGenerate  = false;
        if (userWalletViewList == null || userWalletViewList.size() <= 0) {
            UserWallet userWallet = new UserWallet();
            userWallet.setUserId(param.getUserId());
            userWallet.setWalletAddress("");
            userWallet.setCreatedTime(new Date());
            userWalletDao.add(userWallet);
            needGenerate = true;
        }
        if (userWalletViewList.size() > 0) {
            if(StringUtils.isBlank(userWalletViewList.get(0).getWalletAddress()))
            {
                needGenerate = true;
            }
        }
        if(needGenerate)
        {
            UserWallet walletCriteria = new UserWallet();
            walletCriteria.setUserId(param.getUserId());
            UserWallet existWallet = userWalletDao.get(walletCriteria);
            if (existWallet != null) {

                GenerateUserWalletParameter generateUserWalletParameter = new GenerateUserWalletParameter();
                generateUserWalletParameter.setWalletId(existWallet.getId());
                ObjectResultEx<UserWalletAddressResult>
                        userWalletAddressResult = withdrawBizService.generateUserWallet(generateUserWalletParameter);

                if (userWalletAddressResult.isFailed() || userWalletAddressResult.getDataObject() == null) {
                    return resultEx.makeFailedResult("获取用户钱包地址失败");
                }
                UserWalletAddressResult UserWalletAddress = userWalletAddressResult.getDataObject();
                UserWallet userWalletUpdate = new UserWallet();
                userWalletUpdate.setId(existWallet.getId());
                userWalletUpdate.setUserId(param.getUserId());
                userWalletUpdate.setWalletAddress(UserWalletAddress.getAddress());
                userWalletUpdate.setWalletType("jfc_wallet");
                userWalletUpdate.setUpdatedTime(new Date());
                userWalletDao.update(userWalletUpdate);
                userWalletViewList = userWalletDao.getUserWallet(getUserWalletParameter);
            }
        }
        if(userWalletViewList.size()>0 )
        {
            resultEx.setDataObject(userWalletViewList.get(0));
            resultEx.makeSuccessResult();
            return  resultEx.makeSuccessResult();
        }else
        {
            return resultEx.makeFailedResult("获取用户钱包地址失败");
        }
    }

    /**
     * 钱包提现
     *
     * @param param
     * @return
     */
    @Override
    public ObjectResultEx<WalletWithdrawResult> withdrawWallet(WithdrawWalletParam param) {

        return withdrawBizService.withdrawWallet(param);
    }

    /**
     * 获取JFC当前价格
     *
     * @param param
     * @return
     */
    @Override
    public ObjectResultEx<CoinMarketPriceResult> getCoinCurrentPrice(GetCoinMarketPriceParam param) {

        return transPointBizService.getCoinCurrentPrice(param);
    }
}
